package com.coocaa.ops.plus.dashboard.v1.auth.service.impl;

import cloud.tianai.captcha.spring.application.ImageCaptchaApplication;
import cloud.tianai.captcha.validator.common.model.dto.ImageCaptchaTrack;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.tool.http.HttpSessionUtil;
import com.coocaa.ops.common.tool.json.JsonUtil;
import com.coocaa.ops.common.tool.md5.MD5Util;
import com.coocaa.ops.plus.core.base.constant.PlusConstant;
import com.coocaa.ops.plus.core.base.enums.MyRoleEnums;
import com.coocaa.ops.plus.dashboard.third.AdminPlusProperties;
import com.coocaa.ops.plus.dashboard.v1.auth.model.CaptchaCheckInfo;
import com.coocaa.ops.plus.dashboard.v1.auth.model.MyAccountModel;
import com.coocaa.ops.plus.dashboard.v1.auth.model.PlusConfigInfo;
import com.coocaa.ops.plus.dashboard.v1.auth.service.IPlusLoginService;
import com.coocaa.ops.plus.dashboard.v1.auth.yml.PlusConfigProperties;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.security.NoSuchAlgorithmException;

/**
 * @author bijiahao
 * @date : 2019/6/24.
 * @description 扩展模块-登录验证接口实现
 */
@Service
public class PlusLoginServiceImpl implements IPlusLoginService {
    @Autowired
    private AdminPlusProperties plusLoginProperties;
    @Autowired
    private PlusConfigProperties plusConfigProperties;
    @Autowired
    private ImageCaptchaApplication imageCaptchaApplication;

    @Override
    public PlusConfigInfo getPlusConfigInfo(HttpServletRequest request) {
        PlusConfigInfo plusConfigInfo = new PlusConfigInfo(plusConfigProperties.getDashboardName(), plusConfigProperties.getService());
        Integer localRunning = HttpSessionUtil.isLocal(request) ? 1 : 0;
        plusConfigInfo.setLocalRunning(localRunning);
        return plusConfigInfo;
    }

    @Override
    public R login(MyAccountModel accountModel, HttpServletRequest request) {
        try {
            CaptchaCheckInfo captchaCheckInfo = accountModel.getParam();
            String id = captchaCheckInfo.getId();
            ImageCaptchaTrack imageCaptchaTrack = (ImageCaptchaTrack) JsonUtil.json2Object(captchaCheckInfo.getJsonStr(), ImageCaptchaTrack.class);
            boolean isCaptchaOk = imageCaptchaApplication.matching(id, imageCaptchaTrack).isSuccess();
            if (!isCaptchaOk) {
                return R.fail("验证码错误！");
            }
            if (!isUserNameRight(accountModel.getUsername())) {
                return R.fail("用户名错误！");
            }
            if (!isPasswordRight(accountModel.getPassword())) {
                return R.fail("密码错误！");
            }
            HttpSessionUtil.setSessionValue(request, PlusConstant.SESSION_TOKEN, MyRoleEnums.ROLE_ADMIN.name());
            return R.ok().msg("登录成功！");
        } catch (Exception e) {
            return R.fail("登录异常！", e.getMessage());
        }
    }

    private boolean isUserNameRight(String userName) {
        return StringUtils.isNotBlank(userName) && userName.equals(plusLoginProperties.getLoginName());
    }


    private boolean isPasswordRight(String inputPassword) {
        String toValidPassword = inputPassword + "";
        if (StringUtils.isBlank(inputPassword) && inputPassword.equals(plusLoginProperties.getPassword())) {
            return true;
        }
        if (StringUtils.isBlank(inputPassword) && !inputPassword.equals(plusLoginProperties.getPassword())) {
            return false;
        }
        try {
            toValidPassword = MD5Util.md5(MD5Util.md5(inputPassword + plusLoginProperties.getSalt()));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return false;
        }
        return toValidPassword.equals(plusLoginProperties.getPassword());
    }
}
